import axios from "axios";
import { showLoadingToast } from 'vant';
import baseURL from "./baseURL";
let toast = null
const instance = axios.create({
  baseURL:baseURL,
  timeout:5000
})

// Add a request interceptor
instance.interceptors.request.use(function (config) {
  toast = showLoadingToast({
    message: '加载中...',
    forbidClick: true,
    duration:0
  });
  // Do something before request is sent
  if(config.method.toLowerCase() === 'get'){
    config.params = config.data
  }
  return config;
}, function (error) {
  // Do something with request error
  return Promise.reject(error);
});

// Add a response interceptor
instance.interceptors.response.use(function (response) {
  toast.close();
  // Any status code that lie within the range of 2xx cause this function to trigger
  // Do something with response data
  return response.data;
}, function (error) {
  // Any status codes that falls outside the range of 2xx cause this function to trigger
  // Do something with response error
  return Promise.reject(error);
});


export default instance
